#!/bin/bash

CHAPTER=${1%/}

cp $CHAPTER/index.md $CHAPTER/html.md

mkdir output/$CHAPTER/

for ly in $(ls $CHAPTER/*.ly); do
	name=$(basename $ly .ly)
	sed -e "s|@$name|![]($CHAPTER/$name.png)|g" -i $CHAPTER/html.md
	lilypond -dno-point-and-click --png --output=output/$CHAPTER/ $CHAPTER/$name.ly
done;

for midi in $(ls output/$CHAPTER/*.midi); do
	name=$(basename -s .midi $midi)
	echo $name.midi
	sed -e "s|\[]($name.midi)|<audio controls src=$CHAPTER/$name.ogg />|g" -i $CHAPTER/html.md
	timidity --noise-shaping=1 -Ov output/$CHAPTER/$name.midi
done;

pandoc $CHAPTER/html.md -o output/$CHAPTER.html

rm $CHAPTER/html.md
